from django.shortcuts import render, redirect
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.contrib import auth
from device.models import DeviceInfo
import json


# 登陆
def login(request):
	if request.POST:
		username = password = ''
		username = request.POST.get('username')
		password = request.POST.get('password')
		user = auth.authenticate(username=username, password=password)
		if user is not None and user.is_active:
			auth.login(request, user)
			request.session['user'] = username
			respone = HttpResponseRedirect('/home')
			return respone
		else:
			return render(request, 'login.html', {'error': '用户名或者密码错误'})
	return render(request, 'login.html')


# 对devices进行分页
def paginator_view(request):
	devices_list = DeviceInfo.objects.all()  # 获取所有设备
	paginate = Paginator(devices_list, 3)
	page = request.GET.get('pages')
	try:
		devices = paginate.page(page)
	except PageNotAnInteger:
		devices = paginate.page(1)
	except InvalidPage:
		return HttpResponse('找不到页面内容')
	except EmptyPage:
		devices = paginate.page(paginate.num_pages)
	return render(request, 'device_manage.html', {'pages': devices, 'devices': devices_list})


# 添加设备
def add_device(request):
	if request.method == 'POST':
		device_name = request.POST.get('device_name', '')
		device_type = request.POST.get('device_type')
		device_platform = request.POST.get('device_platform', '')
		cpu_type = request.POST.get('cpu_type', '')
		mem_num = request.POST.get('mem_num')
		level = request.POST.get('level')
		hot = request.POST.get('hot')
		state = request.POST.get('state')
		borrow = request.POST.get('borrow')
		version = request.POST.get('version')
		obj = DeviceInfo.objects.create(device_name=device_name, device_type=device_type,
		                                device_platform=device_platform, cpu_type=cpu_type, mem_num=mem_num,
		                                level=level, hot=hot, state=state, borrow=borrow, version=version)
	return render(request, 'add_device.html')


# 获取所有设备
def get_devices(request):
	devices_list = DeviceInfo.objects.all()  # 获取所有设备
	dict = []
	count = 0
	for device in devices_list:
		dic = {}
		dic['id'] = device.id
		dic['device_name'] = device.device_name
		dic['device_type'] = device.device_type
		dic['device_platform'] = device.device_platform
		dic['cpu_type'] = device.cpu_type
		dic['mem_num'] = device.mem_num
		dic['version'] = device.version
		dic['level'] = device.level
		dic['hot'] = device.hot
		dic['state'] = device.state
		dict.append(dic)
		count = count + 1
	postmsg = {"code": 0, "msg": "", "count": count, "data": dict}
	# 数据以json的格式回调给客户端
	return HttpResponse(json.dumps(postmsg), content_type="application/json")


# 删除设备
# 该注解不确定要不要，暂时不处理，去掉应该也没影响的
@csrf_exempt
def del_device(request):
	result = 0
	if request.method == 'GET':
		id = request.GET.get('id')  # 从客户端获取选中记录的ID
		DeviceInfo.objects.get(id=id).delete()  # 根据ID从数据库中找到该记录，然后执行删除
		result = 200
	return HttpResponse(result)


# 编辑设备
def edit_device(request):
	if request.method == 'GET':
		id = request.GET.get('id')  # 获取从客户端传来的数据
		print('id:', id)
		obj = DeviceInfo.objects.get(id=id)#从数据库中根据ID去到该记录数据
		print('obj:',obj)
		return render(request,'edit_device.html',{'obj':obj})
	else:
		id = request.POST.get('id')  # 获取从客户端传来的数据
		device_name = request.POST.get('device_name', '')
		device_type = request.POST.get('device_type')
		device_platform = request.POST.get('device_platform', '')
		cpu_type = request.POST.get('cpu_type', '')
		mem_num = request.POST.get('mem_num')
		level = request.POST.get('level')
		hot = request.POST.get('hot')
		state = request.POST.get('state')
		borrow = request.POST.get('borrow')
		version = request.POST.get('version')
		DeviceInfo.objects.filter(id=id).update(device_name=device_name, device_type=device_type,
		                                        device_platform=device_platform, cpu_type=cpu_type, mem_num=mem_num,
		                                        level=level, hot=hot, state=state, borrow=borrow, version=version)
		return HttpResponse('200')
	# return render(request, 'edit_device.html')


def index(request):
	return render(request, 'index.html')


def device_manager(request):
	return render(request, 'device_list_manager.html')
